package com.axe.processdatasync;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 数据广播接收
 */
public class MyDataBrodcastReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("com.axe.process.data.action".equals(action)) {
            String data = intent.getStringExtra("data");

            try {
                JSONObject jsonObject = new JSONObject(data);
                int getPid = jsonObject.optInt("pid");
                int pid = android.os.Process.myPid();
                if (getPid == pid) {
                    Toast.makeText(context, "在同一个进程，不同步数据", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "不在同一个进程，不同步数据", Toast.LENGTH_SHORT).show();
                }

                Toast.makeText(context,"广播接收到的数据:"+data,Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
